package com.david.controller.api;

import com.david.entity.Category;
import com.david.model.api.Response;
import com.david.service.CategoryService;
import com.david.utils.DateTimeUtils;
import com.google.common.collect.ImmutableBiMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/api/category/")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @GetMapping("findAll")
    @ApiOperation(value = "获取全部分类", notes = "获取全部分类", tags = "category")
    public Response<Object> findAll() {
        Response<Object> result = new Response<>();
        result.data = categoryService.findAll();
        return result;
    }

    @GetMapping("findCategoryByParentId")
    @ApiOperation(value = "根据ParentId获取", notes = "根据ParentId获取", tags = "category")
    public Response<Object> findCategoryByParentId(Integer parentId) {
        Response<Object> result = new Response<>();
        result.data = categoryService.findCategoryByParentId(parentId);
        return result;
    }

    @GetMapping("findById")
    @ApiOperation(value = "根据id获取分类", notes = "根据id获取分类", tags = "category")
    public Response<Object> findById(Integer id) {
        Response<Object> result = new Response<>();
        result.data = categoryService.findById(id);
        return result;
    }

    @GetMapping("delete")
    @ApiOperation(value = "删除分类", notes = "删除分类", tags = "category")
    public Response<Map> delete(Integer id) {
        Response<Map> result = new Response<>();
        int data = categoryService.delete(id);
        if (data > 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false);
        }
        return result;
    }

    @PostMapping("update")
    @ApiOperation(value = "修改分类信息", notes = "修改分类信息", tags = "category")
    public Response<Map> update(@RequestBody Category category) {
        Response<Map> result = new Response<>();
        int data = categoryService.update(category);
        if (data > 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false);
        }
        return result;
    }

    @PostMapping("insert")
    @ApiOperation(value = "添加分类信息", notes = "添加分类信息", tags = "category")
    public Response<Map> insert(@RequestBody Category category) {
        Response<Map> result = new Response<>();
        int insert = categoryService.insert(category);
        String route = "";
        if (category.parentId > 0) {
            Category parentInfo = categoryService.findById(category.parentId);
            if (parentInfo != null) {
                route = parentInfo.route + category.getId() + ",";
            }
        } else {
            route = "," + category.getId() + ",";
        }

        int update = categoryService.updateRoute(route,category.getId());

        if (insert> 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false);
        }

        return result;
    }
}
